package com.fumin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.web3j.crypto.*;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.exceptions.TransactionException;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Transfer;
import org.web3j.utils.Convert;
import org.web3j.utils.Numeric;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Proxy;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.concurrent.ExecutionException;

/**
 * Created by Lingo on 2018/3/4.
 */
public class Web3JSTest {

    static boolean foo(char c){
        System.out.println(c);
        return true;
    }
    public static void main(String[] args) throws Exception {
        System.out.println(new BigDecimal("0.15").toPlainString());




//        String fileName = WalletUtils.generateNewWalletFile("123456", new File("C:\\Users\\Lingo\\Desktop\\wallet\\"),true);
//        Credentials credentials = WalletUtils.loadCredentials(
//                "123456", "C:\\Users\\Lingo\\Desktop\\wallet\\UTC--2018-03-04T12-18-29.240000000Z--3ad754bef2ee86f25524ba90ade95a688959d116.json");
//        Credentials credentials1 = WalletUtils.loadCredentials(
//                "123456", "C:\\Users\\Lingo\\Desktop\\wallet\\UTC--2018-03-04T12-23-59.919000000Z--21e057510bdf9f375951959d9698f443bc60c3d8.json");
//
//        System.out.println(credentials.getAddress());
//        System.out.println(credentials1.getAddress());
//         Web3j web3j = Web3j.build(new HttpService(
//                "https://rinkeby.infura.io/9HbpOAq5gQFlldpCBRQk"));  // FIXME: Enter your Infura token here;
//
//        EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
//                credentials.getAddress(), DefaultBlockParameterName.LATEST).sendAsync().get();
//        BigInteger nonce = ethGetTransactionCount.getTransactionCount();

//        String url = "https://api.etherscan.io/api?module=proxy&action=eth_getTransactionCount&address=%s&tag=latest&apikey=H6T8WITXFCYG189UEFV2FTNAGWZQIFWHQW";
//        url = String.format(url,credentials1.getAddress());
//        HttpResponse<String> response  = null;
//        response = Unirest.get(url)
//                    .header("cache-control", "no-cache")
//                    .header("postman-token", "01e8236c-7f07-2b64-833e-0a5403afd5d9")
//                    .asString();
//
//        String body = response.getBody();
//        JSONObject resuelt = JSON.parseObject(body);
//        BigInteger nonce = Numeric.toBigInt(resuelt.getString("result"));
//        RawTransaction rawTransaction  = RawTransaction.createEtherTransaction(
//                nonce,
//                Convert.toWei("20", Convert.Unit.GWEI).toBigInteger(),
//                BigDecimal.valueOf(25200).toBigInteger() ,
//                credentials.getAddress(),
//                Convert.toWei("0.01", Convert.Unit.ETHER).toBigInteger()
//        );
//        byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials1);
//        String hexValue = Numeric.toHexString(signedMessage);
//        System.out.println(hexValue);
//        String transactionUrl = "https://api.etherscan.io/api?module=proxy&action=eth_sendRawTransaction&hex=%s&apikey=H6T8WITXFCYG189UEFV2FTNAGWZQIFWHQW";
//        transactionUrl=  String.format(transactionUrl,hexValue);
//        try {
//            response = Unirest.get(transactionUrl)
//                    .asString();
//        } catch (UnirestException e) {
//
//        }
//        response.getBody();
//        body = response.getBody();
//        resuelt = JSON.parseObject(body);
//        System.out.println(resuelt);
//        System.out.println(resuelt.getString("result"));



//        EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
//        String transactionHash = ethSendTransaction.getTransactionHash();
//        System.out.println("transactionHash: "+transactionHash);

    }


}
